bg1 = new Image();
bg1.src = "./images/button_bg1.png";
bg2 = new Image();
bg2.src = "./images/button_bg2.png";
bg3 = new Image();
bg3.src = "./images/button_bg3.png";

tabs1 = new Image();
tabs1.src = "./images/tabs1.png";
tabs2 = new Image();
tabs2.src = "./images/tabs2.png";
tabs3 = new Image();
tabs3.src = "./images/tabs3.png";
tabs4 = new Image();
tabs4.src = "./images/tabs4.png";

var resObject;
var frameDoc;
var frameWin;
var foregroundColorWindow;
var backgroundColorWindow;
var mode; //0:ActiveX1 (IE7) / 1:ActiveX2 / 2:XMLHttpRequest (Firefox)

var viewMode = 0; //0:Text / 1:HTML

function init()
{
	try
	{
		resObject = new ActiveXObject("Microsoft.XMLHTTP");
		mode = 0;
	}
	catch(Error)
	{
		try
		{
			resObject = new ActiveXObject("MSXML2.XMLHTTP");
			mode = 1;
		}
		catch(Error)
		{
			try
			{
				resObject = new XMLHttpRequest();
				mode = 2;
			}
			catch(Error)
			{
				alert("Could not initialise the XMLHttpRequest-Object.");
			}
		}
	}

	if(navigator.appName.search("Microsoft") > -1)
	{
		frameDoc = frame.document;
		frameWin = frame.window;
	}
	else
	{
		frameDoc = this.document.getElementById('frame').contentDocument;
		frameWin = this.document.getElementById('frame').contentWindow;
	}

	frameDoc.designMode = 'On';
}

function changeBackgroundImage(field_id, image)
{
	this.document.getElementById(field_id).style.backgroundImage = "url("+image.src+")";
}

function tabsMouseOver()
{
	if(viewMode==0)
	{
		this.document.getElementById('tabs').src = tabs2.src;
	}
	else
	{
		this.document.getElementById('tabs').src = tabs4.src;
	}
}

function tabsMouseOut()
{
	if(viewMode==0)
	{
		this.document.getElementById('tabs').src = tabs1.src;
	}
	else
	{
		this.document.getElementById('tabs').src = tabs3.src;
	}
}

function tabsMouseDown()
{
	viewMode = 1 - viewMode;

	if(viewMode==0)
	{
		this.document.getElementById('tabs').src = tabs2.src;
		this.document.getElementById('tabs').title = 'switch to HTML view';

		if(mode==2)
		{
			frameDoc.body.innerHTML = frameDoc.body.textContent;
		}
		else
		{
			frameDoc.body.innerHTML = frameDoc.body.innerText;
		}
	}
	else
	{
		this.document.getElementById('tabs').src = tabs4.src;
		this.document.getElementById('tabs').title = 'switch to text view';

		if(mode==2)
		{
			frameDoc.body.textContent = frameDoc.body.innerHTML;
		}
		else
		{
			frameDoc.body.innerText = frameDoc.body.innerHTML;
		}
	}
}

function doBold()
{
	frameDoc.execCommand('bold', false, null);
}

function doItalic()
{
	frameDoc.execCommand('italic', false, null);
}

function doUnderline()
{
	frameDoc.execCommand('underline', false, null);
}

function doAlignLeft()
{
	frameDoc.execCommand('justifyleft', false, null);
}

function doAlignCenter()
{
	frameDoc.execCommand('justifycenter', false, null);
}

function doAlignRight()
{
	frameDoc.execCommand('justifyright', false, null);
}

function insertOrderedList()
{
	frameDoc.execCommand('insertorderedlist', false, null);
}

function insertUnorderedList()
{
	frameDoc.execCommand('insertunorderedlist', false, null);
}

function insertTable(rows, cols, border, cellpadding, cellspacing)
{
	if(mode==2)
	{
		insertTableFF(rows, cols, border, cellpadding, cellspacing);
	}
	else
	{
		insertTableIE(rows, cols, border, cellpadding, cellspacing);
	}
}

function insertTableIE(rows, cols, border, cellpadding, cellspacing)
{
	frameWin.focus();

	var html = '<table border="'+border+'" cellpadding="'+cellpadding+'" cellspacing="'+cellspacing+'"><tbody>';

	for(var r=0; r<rows; r++)
	{
		html += '<tr>';
		for(var c=0; c<cols; c++)
		{
			html += '<td></td>';
		}
		html += '</tr>';
	}

	html += '</tbody></table>';
	frameDoc.selection.createRange().pasteHTML([html].join('\r\n'));

}

function insertTableFF(rows, cols, border, cellpadding, cellspacing)
{
	if ((rows>0) && (cols>0))
	{
		table = frameDoc.createElement("table");
		table.setAttribute("border", border);
		table.setAttribute("cellpadding", cellpadding);
		table.setAttribute("cellspacing", cellspacing);
		tbody = frameDoc.createElement("tbody");
		for (var r=0; r<rows; r++)
		{
			tr =frameDoc.createElement("tr");
			for (var c=0; c<cols; c++)
			{
				td =frameDoc.createElement("td");
				br =frameDoc.createElement("br");
				td.appendChild(br);
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);      
		insertNodeAtSelection(frameWin, table);
	}
}
	
function openForegroundColor()
{
	foregroundColorWindow = window.open( "./foregroundcolor.php" , "foregroundColorWindow" , "width=480, height=580, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes");
}

function openBackgroundColor()
{
	backgroundColorWindow = window.open( "./backgroundcolor.php" , "backgroundColorWindow" , "width=480, height=580, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes");
}

function openTable()
{
	backgroundColorWindow = window.open( "./table.php" , "tableWindow" , "width=230, height=350, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes");
}

function setForegroundColor(red, green, blue)
{
	frameDoc.execCommand('forecolor', false, '#'+getHex(red)+getHex(green)+getHex(blue));
}

function setBackgroundColor(red, green, blue)
{
	if(mode==2)
	{
		frameDoc.execCommand('hilitecolor', false, '#'+getHex(red)+getHex(green)+getHex(blue));
	}
	else
	{
		frameDoc.execCommand('backcolor', false, '#'+getHex(red)+getHex(green)+getHex(blue));
	}
}

function getHex(number)
{
	chr = new Array(16);

	chr[0] = '0';
	chr[1] = '1';
	chr[2] = '2';
	chr[3] = '3';
	chr[4] = '4';
	chr[5] = '5';
	chr[6] = '6';
	chr[7] = '7';
	chr[8] = '8';
	chr[9] = '9';
	chr[10] = 'A';
	chr[11] = 'B';
	chr[12] = 'C';
	chr[13] = 'D';
	chr[14] = 'E';
	chr[15] = 'F';

	left = 0;
	right = 0;

	while((16*left+right) < number)
	{
		right++;
		if(right > 15)
		{
			left++;
			right = 0;
		}
	}

	return chr[left]+chr[right];
}

function insertNodeAtSelection(win, insertNode)
{	
	var selection = win.getSelection();
	var range = selection.getRangeAt(0);
	selection.removeAllRanges();

	range.deleteContents();

	var container = range.startContainer;
	var pos = range.startOffset;

	range=document.createRange();

	if (container.nodeType==3 && insertNode.nodeType==3)
	{
		container.insertData(pos, insertNode.nodeValue);
		range.setEnd(container, pos+insertNode.length);
		range.setStart(container, pos+insertNode.length);
	}
	else
	{
		var afterNode;
		if (container.nodeType==3)
		{
			var textNode = container;
			container = textNode.parentNode;
			var text = textNode.nodeValue;
			var textBefore = text.substr(0,pos);
			var textAfter = text.substr(pos);
			var beforeNode = document.createTextNode(textBefore);
			afterNode = document.createTextNode(textAfter);

			container.insertBefore(afterNode, textNode);
			container.insertBefore(insertNode, afterNode);
			container.insertBefore(beforeNode, insertNode);
			container.removeChild(textNode);
		}
		else
		{
			afterNode = container.childNodes[pos];
			container.insertBefore(insertNode, afterNode);
		}
		range.setEnd(afterNode, 0);
		range.setStart(afterNode, 0);
	}
	selection.addRange(range);
}


